package game.store
{
	import engine.core.*;
	import game.messages.inventory.*;
	import game.messages.store.*;
	import game.network.*;
	
	public class RxStore
	{
		private var m_store:RxStore_RetrieveStoreResponseMessage;
		private var m_retrieveStoreCallback:Function;
		private var m_callbackMap:Object;
		private static var s_instance:RxStore;
		
		public function RxStore()
		{
			this.m_store = new RxStore_RetrieveStoreResponseMessage();
			s_instance = this;
			this.m_callbackMap = {};
			this.Reset();
		}
		
		public function Reset() : void
		{
			RxTransactionServer.instance.RemoveListener(RxStore_PurchaseResponseMessage.messageId, this.OnPurchaseResponse);
			RxTransactionServer.instance.RemoveListener(RxStore_RetrieveStoreResponseMessage.messageId, this.OnRetrieveStoreResponseMessage);
			RxTransactionServer.instance.AddListener(RxStore_PurchaseResponseMessage.messageId, this.OnPurchaseResponse);
			RxTransactionServer.instance.AddListener(RxStore_RetrieveStoreResponseMessage.messageId, this.OnRetrieveStoreResponseMessage);
		}
		
		private function OnPurchaseResponse(param1:RxStore_PurchaseResponseMessage) : void
		{
			var _loc_2:Function = null;
			if (this.m_callbackMap.hasOwnProperty(param1.offerId))
			{
				_loc_2 = this.m_callbackMap[param1.offerId];
				if (_loc_2 != null)
				{
					this.m_callbackMap[param1.offerId] = null;
					delete this.m_callbackMap[param1.offerId];
					_loc_2(this, param1);
				}
			}
			else
			{
				RxLog.Error("Received a purchase response with a proper callback.");
			}
			return;
		}
		
		private function OnRetrieveStoreResponseMessage(param1:RxStore_RetrieveStoreResponseMessage) : void
		{
			this.m_store = param1;
			this.FetchItems(param1);
			var _loc_2:* = this.m_retrieveStoreCallback;
			this.m_retrieveStoreCallback = null;
			if (_loc_2 != null)
			{
				_loc_2(this);
			}
		}
		
		private function FetchItems(param1:RxStore_RetrieveStoreResponseMessage) : void
		{
			var _loc_2:RxStore_AisleInfo = null;
			var _loc_3:RxStore_OfferInfo = null;
			var _loc_4:uint = 0;
			for each (_loc_2 in param1.aisles)
			{
				
				for each (_loc_3 in _loc_2.offers)
				{
					
					for each (_loc_4 in _loc_3.items)
					{
						
						RxNetIDManager.instance.Fetch(_loc_4);
					}
				}
			}
		}
		
		public function RetrieveStoreFromServer(param1:Function) : void
		{
			this.m_retrieveStoreCallback = param1;
			var _loc_2:* = new RxStore_RetrieveStoreRequestMessage();
			RxTransactionServer.instance.SendMessage(_loc_2);
		}
		
		public function GetAisles() : Vector.<RxStore_AisleInfo>
		{
			return this.m_store.aisles;
		}
		
		public static function get instance() : RxStore
		{
			return s_instance;
		}
		
		public static function GetCurrencyFromString(param1:String) : int
		{
			switch(param1)
			{
				case RxInventory_ItemNameConstants.ITEMNAME_GOLD:
				{
					return RxStore_OfferInfo.CURRENCY_GOLD;
				}
				case RxInventory_ItemNameConstants.ITEMNAME_KEY:
				{
					return RxStore_OfferInfo.CURRENCY_KEYS;
				}
				case "platinum":
				{
					return RxStore_OfferInfo.CURRENCY_PLATINUM;
				}
				default:
				{
					break;
					break;
				}
			}
			return -1;
		}
		
		public function RequestPurchase(param1:RxStore_OfferInfo, param2:String, param3:Function) : void
		{
			/*
			var _loc_5:RxStore_PurchaseResponseMessage = null;
			if (param1 == null || param1.offerId == null || param1.offerId.length == 0)
			{
				if (param3 != null)
				{
					_loc_5 = new RxStore_PurchaseResponseMessage();
					_loc_5.response = RxStore_PurchaseResponseMessage.RESPONSE_FAILURE_INVALID_OFFER;
					this.param3(this, _loc_5);
				}
				return;
			}
			this.AddPurchaseCallback(param1.offerId, param3);
			var _loc_4:* = new RxStore_PurchaseRequestMessage();
			new RxStore_PurchaseRequestMessage().offerId = param1.offerId;
			_loc_4.sourceId = param2;
			RxTransactionServer.instance.SendMessage(_loc_4);
			*/
		}
	}
}